Skip to content

perf: remove Before ordering constraint on dde-session-daemon from XS…#67

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
yixinshark:perf/remove-xsettings-before-daemon-constraint
May 15, 2026
Merged

perf: remove Before ordering constraint on dde-session-daemon from XS…#67
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
yixinshark:perf/remove-xsettings-before-daemon-constraint

Conversation

@yixinshark

@yixinshark yixinshark commented May 13, 2026

Copy link
Copy Markdown
Contributor

…ettings1 service

Remove the Before=org.dde.session.Daemon1.service ordering constraint from the XSettings1 D-Bus service unit. This allows dde-session-daemon to start in parallel with XSettings1, reducing startup serialization on the critical path.

移除 XSettings1 D-Bus 服务单元中对 dde-session-daemon 的 Before 排序约束, 使得 dde-session-daemon 可以与 XSettings1 并行启动,减少关键路径上的串行化等待。

Log: perf: remove Before ordering constraint on dde-session-daemon from XSettings1 service
Change-Id: I4fec93594441957f8ef21f5bcd1b43f0c228568c

Summary by Sourcery

Enhancements:

  • Remove the Before= dependency on dde-session-daemon from the XSettings1 D-Bus service to reduce unnecessary startup serialization.

@sourcery-ai

sourcery-ai Bot commented May 13, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Removes the systemd/D-Bus unit ordering constraint that forced org.deepin.dde.XSettings1.service to start before dde-session-daemon, allowing both services to start in parallel and reducing startup serialization.

File-Level Changes

Change Details Files
Relaxed service startup ordering so XSettings1 no longer must start before dde-session-daemon.
  • Removed the Before=org.dde.session.Daemon1.service directive from the XSettings1 D-Bus service unit configuration
  • Left other service unit directives unchanged to preserve existing behavior aside from the ordering constraint
src/plugin-qt/xsettings/misc/org.deepin.dde.XSettings1.service

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@yixinshark yixinshark force-pushed the perf/remove-xsettings-before-daemon-constraint branch from d89bcc1 to 4aa0fb3 Compare May 14, 2026 07:44
mhduiy
mhduiy previously approved these changes May 14, 2026
@yixinshark yixinshark force-pushed the perf/remove-xsettings-before-daemon-constraint branch 4 times, most recently from 7618409 to 4da4e20 Compare May 15, 2026 01:31
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: robertkill, yixinshark

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

…dundant init, clean up debug code

Remove Before=org.dde.session.Daemon1.service so XSettings1 and dde-session-daemon
start in parallel. On subsequent boots where scale factor is already persisted in
DConfig, skip RandR monitor enumeration, Plymouth scaling, and Greeter Qt theme
update (~100-350ms saved). Remove leftover debug code in getXcbAtomProperty that
queried atom type names and hex-dumped binary property values on every call.
Remove unnecessary isServiceRegistered guard in DSMRegister. Add startup timing
logs (qWarning level) for future profiling.

移除对 dde-session-daemon 的 Before 排序约束,使 XSettings1 和 dde-session-daemon
可以并行启动。后续登录时跳过 RandR 枚举、Plymouth 缩放和 Greeter 更新
(节省约 100-350ms)。清理 getXcbAtomProperty 中的调试遗留代码(每次读取属性
都查询原子类型名称并 hex 转储二进制值)。移除 DSMRegister 中不必要的
isServiceRegistered 检查。添加启动计时日志用于性能分析。

Log: perf: optimize XSettings1 startup — remove Before constraint, skip redundant init, clean up debug code
Change-Id: I2b5f4e9404bc2af10e077d936698a2b2569d2a8e
@yixinshark yixinshark force-pushed the perf/remove-xsettings-before-daemon-constraint branch from 4da4e20 to 0bccfd4 Compare May 15, 2026 01:33
@yixinshark

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented May 15, 2026

Copy link
Copy Markdown

This pr force merged! (status: blocked)

@deepin-bot deepin-bot Bot merged commit 55beb70 into linuxdeepin:master May 15, 2026
7 of 9 checks passed
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeex,很高兴为你审查这段代码。

本次审查主要涉及两个文件的修改:一是 xsettingsmanager.cpp 中对缩放因子初始化逻辑的调整,二是 .service 文件中单元依赖关系的变更。下面我将从语法逻辑、代码质量、代码性能和代码安全四个方面为你进行详细分析,并提供改进意见。


1. 语法与逻辑

xsettingsmanager.cpp 逻辑分析:

  • 潜在的类型与精度问题:代码使用 double scale = m_settingDconfig->value("scale-factor").toDouble(); 来获取缩放因子。在 X11/Xsettings 的底层实现中,缩放因子通常是以整数乘以 1024 的形式存储和计算的(即 scale * 1024)。使用 toDouble() 可能会在后续转换时引入浮点数精度问题。如果 DConfig 底层存储的是整型,建议使用 toInt() 并进行相应的除法运算,或者确认框架层面已完美兼容 toDouble()
  • 逻辑分支的完整性:新增的 if (scale <= 0) 分支处理了无效配置的情况,逻辑是清晰且合理的。但需要注意的是,当 scale > 0 时,代码跳过了 setScreenScaleFactorsadjustScaleFactor,直接执行了 setSettingsupdateDPI。请确认:如果用户配置了有效的 scale-factor,是否仍然需要调用 setScreenScaleFactors(getScreenScaleFactors(), false); 来同步其他屏幕的因子?如果不需要,当前逻辑是正确的;如果需要,这里就存在逻辑遗漏。

org.deepin.dde.XSettings1.service 逻辑分析:

  • 依赖关系变更:删除了 Before=org.dde.session.Daemon1.service。这意味着 XSettings1 不再强制在 Daemon1 之前启动。请务必确认 Daemon1 在启动时是否依赖 XSettings1 提供的 DPI 或缩放环境变量。如果 Daemon1 启动时读取不到正确的缩放比例,可能会导致界面渲染错乱。此修改需要结合系统启动时序图进行严格测试。

2. 代码质量

  • 魔法数字scale <= 0 中的 0 属于魔法数字。虽然在这里语义相对明显,但为了代码的健壮性和可读性,建议将其定义为常量。
  • 变量作用域scale 变量在 if (m_settingDconfig->isValid()) 外部声明,并在内部赋值。虽然 C++ 允许这样写,但将变量的声明和初始化尽量靠近使用的地方,或者直接在判断逻辑中处理,会使代码更紧凑。
  • 注释规范:新增的注释 // scale <= 0 is invalid, use recommended scale factor 是一个很好的实践,建议继续保持。

3. 代码性能

  • 函数调用开销:原代码无条件调用 getScreenScaleFactors()getRecommendedScaleFactor(),新代码在 scale > 0 时跳过了这些调用。这是一个正向的性能优化,避免了不必要的计算和 X11 请求。
  • DConfig 读取m_settingDconfig->value("scale-factor") 通常涉及跨进程或文件的 I/O 读取(取决于 DConfig 的后端实现)。在初始化阶段调用一次是不可避免的,性能影响可以接受。

4. 代码安全

  • 空指针与野指针:代码中使用了 m_settingDconfig->isValid() 进行了有效性检查,这是非常棒的安全防范,避免了在 DConfig 对象无效时调用 value() 导致的崩溃。
  • 除零风险:如果后续代码(如 updateDPI() 或 Qt 内部)使用 scale 作为除数,当前 scale <= 0 的拦截有效防止了除零异常。
  • 配置篡改scale-factor 来自配置中心,属于外部输入。如果用户恶意篡改配置文件,写入了极大的值(如 10000)或极小的正数(如 0.00001),可能会导致 GUI 界面不可用或内存激增。建议增加对 scale 合理范围(如 [1.0, 3.0])的校验。

改进建议代码

综合以上分析,我为你提供一份优化后的代码参考:

diff --git a/src/plugin-qt/xsettings/impl/xsettingsmanager.cpp b/src/plugin-qt/xsettings/impl/xsettingsmanager.cpp
index b2acb39..2263d58 100644
--- a/src/plugin-qt/xsettings/impl/xsettingsmanager.cpp
+++ b/src/plugin-qt/xsettings/impl/xsettingsmanager.cpp
@@ -30,8 +30,24 @@ XSettingsManager::XSettingsManager(QObject *parent)
     , m_xcbUtils(XcbUtils::getInstance())
 {
     connect(m_settingDconfig, &DTK_CORE_NAMESPACE::DConfig::valueChanged, this, &XSettingsManager::handleDConfigChangedCb);
-    setScreenScaleFactors(getScreenScaleFactors(), false);
-    adjustScaleFactor(getRecommendedScaleFactor());
+
+    // 定义缩放因子的合理范围,防止配置被恶意篡改导致UI异常
+    static constexpr double MIN_VALID_SCALE = 1.0;
+    static constexpr double MAX_VALID_SCALE = 3.0; // 请根据实际业务需求调整最大值
+
+    double scale = 0;
+    if (m_settingDconfig->isValid()) {
+        scale = m_settingDconfig->value("scale-factor").toDouble();
+    }
+
+    // scale <= 0 or out of bounds is invalid, use recommended scale factor
+    if (scale < MIN_VALID_SCALE || scale > MAX_VALID_SCALE) {
+        setScreenScaleFactors(getScreenScaleFactors(), false);
+        adjustScaleFactor(getRecommendedScaleFactor());
+    } else {
+        // TODO: 确认当 scale 有效时,是否需要调用 setScreenScaleFactors 同步其他屏幕参数?
+        // 如果需要,请在此处补充:setScreenScaleFactors(...);
+    }
+
     setSettings(setSettingsInSchema());
 
     updateDPI();

关于 .service 文件的修改提醒:
对于删除 Before=org.dde.session.Daemon1.service,代码层面无需修改,但必须在测试阶段重点验证 DDE 会话启动的竞态条件,确保 Daemon1 服务在启动时不会因为 XSettings 未就绪而读取到错误的 DPI/Scale 默认值。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants